import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";

const $axios: AxiosInstance = axios.create({
  baseURL: process.env.VUE_APP_BASE_URL as string,
  timeout: 5000,
});

$axios.interceptors.request.use(
  (config: AxiosRequestConfig) => {
    console.log(config);
    if ((config.url as string).indexOf(".json") !== -1) {
      config.baseURL = process.env.vue_app_current_url;
    }

    return config;
  },
  (id: number) => {
    console.log(id);
  }
);
$axios.interceptors.response.use(
  (response: AxiosResponse) => {
    return response.data;
  },
  (id: number) => {
    console.log(id, "response");
  }
);

export const getAction = (url: string, params = {}, headers = {}) => {
  return $axios({
    url: url,
    method: "GET",
    params,
    headers,
  });
};
export default $axios;
